<?php
class ModelUploadProduct extends Model {
	public function addProduct($data) {
		//-------------------------------------------------------------------------//
		// get Shop Id
		//-------------------------------------------------------------------------//
		$shop_id = "";
		$this->load->model('upload/shop');
		$this->load->model('upload/category');
		$customer_id = $this->customer->getId();
		$shopInfo    = $this->model_upload_shop->getShopIdByCustomerId($customer_id);
		//-------------------------------------------------------------------------//
		// Insert data into Shops table
		//-------------------------------------------------------------------------//
		if (isset($data['shop_name'])
			&& empty($data['customer_id']) == false
			&&(empty($shopInfo) == true))
		{
			$rs = $this->model_upload_shop->createShop($data);
			//-------------------------------------------------------------------------//
			// Get shop_id
			//-------------------------------------------------------------------------//
			if(empty($rs) == false){
				$shopInfo = $this->model_upload_shop->getShopId($customer_id,
														$data['shop_name']);
			}
		}
		if(empty($shopInfo) == false){
			$shop_id = $shopInfo[0]['shop_id'];
		}
		//-------------------------------------------------------------------------//
		// Get catalog
		//-------------------------------------------------------------------------//
		$catalog_child  = "";
		$catalog_parent = "";
		$catalogStr     = "";

		if(isset($data["catalog_child"]) && mb_strlen($data["catalog_child"]) > 0){
			$catalog_child = $data["catalog_child"];
		}
		if(isset($data["catalog_parent"]) && mb_strlen($data["catalog_parent"]) > 0){
			$catalog_parent = $data["catalog_parent"];
		}

		//-------------------------------------------------------------------------//
		// calculator price
		//-------------------------------------------------------------------------//
		$price = (float)str_replace(".","",$data['price']);
		if(strtoupper(trim($data['price_type'])) == "USA" ){
			$price = $price * 17000;
		}
		//-------------------------------------------------------------------------//
		// insert model into product tbl
		//-------------------------------------------------------------------------//
		// Hoa tran add hard code for initial data
	    $data['model']           = "upload";
	    $data['stock_status_id'] = 7; // in stock
	    $data['status']          = 1; // validate product
	    $tax_class_id 			 = 1;

		// Build SQL
		$SQL_Pr  = "INSERT INTO " . DB_PREFIX . "product SET ";
		$SQL_Pr .= " model = '" . $this->db->escape($data['model']) ."',";
		$SQL_Pr .= " stock_status_id = '" . (int)$data['stock_status_id'] . "',";
		$SQL_Pr .= " price = '". $price. " ', ";
		$SQL_Pr .= " status = '" . (int)$data['status'] . "',";
		$SQL_Pr .= " payment_id = '" . (int)$data['payment'] . "',";
		$SQL_Pr .= " sale_flg = '" . (int)$data['sale_flg'] . "',";
		$SQL_Pr .= " shop_id = '" . (int)$shop_id . "',";
		$SQL_Pr .= " user_flag     = 1 , ";
		$SQL_Pr .= " date_modified = NOW(),";
		$SQL_Pr .= " date_added    = NOW()";
		//-------------------------------------------------------------------------//
		// Insert data into product table
		//-------------------------------------------------------------------------//
		$this->db->query($SQL_Pr);
		$product_id = $this->db->getLastId();

		//-------------------------------------------------------------------------//
		// insert product for parent catalog data into catalog table
		//-------------------------------------------------------------------------//
		$this->load->model('product_to_catalog/select');
		$isDup    = $this->model_product_to_catalog_select->checkDuplicatePC((int)$product_id,(int)$catalog_parent);
	    if($isDup <= 0){
			$SQL_Cata1  = " INSERT INTO " . DB_PREFIX . " product_to_category SET ";
			$SQL_Cata1 .= " product_id = '"  . (int)$product_id . "', ";
			$SQL_Cata1 .= " category_id = '" . (int)$catalog_parent . "'";
			$this->db->query($SQL_Cata1);
		}

		//-------------------------------------------------------------------------//
		// insert data into product_to_category table
		//-------------------------------------------------------------------------//
		$SQL_Cata  = " INSERT INTO " . DB_PREFIX . " product_to_category SET ";
		$SQL_Cata .= " product_id = '"  . (int)$product_id . "', ";
		$SQL_Cata .= " category_id = '" . (int)$catalog_child . "'";
		$this->db->query($SQL_Cata);

		//-------------------------------------------------------------------------//
		// Insert data into product_description
		//-------------------------------------------------------------------------//
		// Hoa hard code default languge id is vn
		$language_id = '1';
		$SQl_dcr = "INSERT INTO " . DB_PREFIX . "product_description SET ";
		$SQl_dcr .=	"product_id = '" . (int)$product_id . "', ";
		$SQl_dcr .=	"language_id = '" . (int)$language_id . "', ";
		$SQl_dcr .=	"name = '" . $this->db->escape($data['name']) . "', ";
		$SQl_dcr .=	"description = '" . $this->db->escape($data['detail_info']) . "'";
		$this->db->query($SQl_dcr);
		$product_id = $this->db->getLastId();

		//-------------------------------------------------------------------------//
		// Set image name into Product table
		//-------------------------------------------------------------------------//
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "product SET image = '" . $this->db->escape($data['image']) . "' WHERE product_id = '" . (int)$product_id . "'");
		}

		//-------------------------------------------------------------------------//
		// Insert data into shop_to_catalog table
		//-------------------------------------------------------------------------//
		if(mb_strlen(trim($shop_id)) > 0 && mb_strlen(trim($catalog_child)) > 0){
			$dup_flg = $this->model_upload_shop->checkDuplicate($shop_id, $catalog_child);
			// do not exist couple shop_id and catalog_id in shop_to_catolog table
			if($dup_flg <= 0){
				$sql = " INSERT INTO " . DB_PREFIX . " shop_to_category SET ";
				$sql .= " category_id = '" . (int)$catalog_child . "'," ;
				$sql .= " shop_id = '" . (int)$shop_id . "'";
				$this->db->query($sql);
			}
		}

		//$parent_seo = "";
		//$child_seo  = "";
		//$parent_seo = $this->model_upload_category->getCategoryName($catalog_parent);
		//$child_seo  = $this->model_upload_category->getCategoryName($catalog_child);

		//-------------------------------------------------------------------------//
		// Insert data into alias URL table
		//-------------------------------------------------------------------------//
		if(mb_strlen(trim($product_id)) > 0
			&& mb_strlen($data['name']) > 0){
			$query 	   = "";
			$query .= "product_id=".$product_id;
			$sql1  = " INSERT INTO " . DB_PREFIX . " url_alias SET ";
			$sql1 .= " query = '" . $query . "'," ;
			$sql1 .= " keyword = '" . $this->convertToSeo($data['name']) . ".html'";
			$this->db->query($sql1);
		}
		return $product_id;

//		if (isset($data['product_option'])) {
//			foreach ($data['product_option'] as $product_option) {
//				$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', sort_order = '" . (int)$product_option['sort_order'] . "'");
//
//				$product_option_id = $this->db->getLastId();
//
//				foreach ($product_option['language'] as $language_id => $language) {
//					$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_description SET product_option_id = '" . (int)$product_option_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
//				}
//
//				if (isset($product_option['product_option_value'])) {
//					foreach ($product_option['product_option_value'] as $product_option_value) {
//						$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value SET product_option_id = '" . (int)$product_option_id . "', product_id = '" . (int)$product_id . "', price = '" . (float)$product_option_value['price'] . "', prefix = '" . $this->db->escape($product_option_value['prefix']) . "', sort_order = '" . (int)$product_option_value['sort_order'] . "'");
//
//						$product_option_value_id = $this->db->getLastId();
//
//						foreach ($product_option_value['language'] as $language_id => $language) {
//							$this->db->query("INSERT INTO " . DB_PREFIX . "product_option_value_description SET product_option_value_id = '" . (int)$product_option_value_id . "', language_id = '" . (int)$language_id . "', product_id = '" . (int)$product_id . "', name = '" . $this->db->escape($language['name']) . "'");
//						}
//					}
//				}
//			}
//		}
//
//		if (isset($data['product_discount'])) {
//			foreach ($data['product_discount'] as $value) {
//				$this->db->query("INSERT INTO " . DB_PREFIX . "product_discount SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$value['customer_group_id'] . "', quantity = '" . (int)$value['quantity'] . "', price = '" . (float)$value['price'] . "', date_start = '" . $this->db->escape($value['date_start']) . "', date_end = '" . $this->db->escape($value['date_end']) . "', priority = '" . $this->db->escape($value['priority']) . "''");
//			}
//		}
//
//		if (isset($data['product_image'])) {
//			foreach ($data['product_image'] as $image) {
//        		$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . "', image = '" . $this->db->escape($image) . "'");
//			}
//		}
//
//		if (isset($data['product_download'])) {
//			foreach ($data['product_download'] as $download_id) {
//				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_download SET product_id = '" . (int)$product_id . "', download_id = '" . (int)$download_id . "'");
//			}
//		}
//
//		if (isset($data['product_category'])) {
//			foreach ($data['product_category'] as $category_id) {
//				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
//			}
//		}
//
//		if (isset($data['product_related'])) {
//			foreach ($data['product_related'] as $related_id) {
//				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'");
//			}
//		}
//
//		if ($data['keyword']) {
//			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
//		}

		$this->cache->delete('product');
	}

	//-------------------------------------------------------------------------//
	// Edit Product
	//-------------------------------------------------------------------------//
	public function editProduct($product_id, $data) {
echo "<pre>";
print_r($data);
echo "</pre>";
exit;
		//-------------------------------------------------------------------------//
		// update into product
		//-------------------------------------------------------------------------//
	    $SQL_Product = "UPDATE " . DB_PREFIX . "product SET " .
			" model = '" . $this->db->escape($data['model']) . "', " .
			" quantity = '" . (int)$data['quantity'] . "', " .
			" stock_status_id = '" . (int)$data['stock_status_id'] . "', " .
			" date_available = '" . $this->db->escape($data['date_available']) . "', " .
			" manufacturer_id = '" . (int)$data['manufacturer_id'] . "', " .
			" shipping = '" . (int)$data['shipping'] . "', " .
		    " price = '" . (float)$data['price'] . "', " .
    		" sort_order = '" . (int)$data['sort_order'] . "', " .
			" weight = '" . (float)$data['weight'] . "', " .
			" weight_class_id = '" . (int)$data['weight_class_id'] . "', " .
			" length = '" . (float)$data['length'] . "', " .
			" width = '" . (float)$data['width'] . "', " .
			" height = '" . (float)$data['height'] . "', " .
			" measurement_class_id = '" . (int)$data['measurement_class_id'] . "', " .
			" status = '" . (int)$data['status'] . "', " .
			" tax_class_id = '" . (int)$data['tax_class_id'] . "', " .
			" date_modified = NOW() " .
			" WHERE product_id = '" . (int)$product_id . "'";
		$this->db->query($SQL_Product);

		//-------------------------------------------------------------------------//
		// update image into product
		//-------------------------------------------------------------------------//
		if (isset($data['image'])) {
			$SQL_Image = " UPDATE " . DB_PREFIX . "product SET " .
				" image = '" . $this->db->escape($data['image']) . "' " .
				" WHERE product_id = '" . (int)$product_id . "'";
			$this->db->query($SQL_Image);
		}
		//-------------------------------------------------------------------------//
		// delete from product_description
		//-------------------------------------------------------------------------//
		$SQL3 = " DELETE FROM " . DB_PREFIX . "product_description " .
				" WHERE product_id = '" . (int)$product_id . "'";
		$this->db->query($SQL3);

		foreach ($data['product_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		//-------------------------------------------------------------------------//
		// product to catalog
		//-------------------------------------------------------------------------//
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");

		if (isset($data['product_category'])) {
			foreach ($data['product_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");

		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}

		$this->cache->delete('product');
	}

	public function deleteProduct($product_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_option_description WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_option_value_description WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "review WHERE product_id = '" . (int)$product_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id. "'");

		$this->cache->delete('product');
	}

	public function getProduct($product_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id . "') AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->language->getId() . "'");

		return $query->row;
	}

	public function getProducts($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->language->getId() . "'";

			if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
				$sql .= " AND pd.name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
			}

			if (isset($data['filter_model']) && !is_null($data['filter_model'])) {
				$sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
			}

			if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
				$sql .= " AND p.quantity = '" . $this->db->escape($data['filter_quantity']) . "'";
			}

			if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
				$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
			}

			$sort_data = array(
				'pd.name',
				'p.model',
				'p.quantity',
				'p.status',
				'p.sort_order'
			);

			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY pd.name";
			}

			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}

			if (isset($data['start']) || isset($data['limit'])) {
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}

			$query = $this->db->query($sql);

			return $query->rows;
		} else {
			$product_data = $this->cache->get('product.' . $this->language->getId());

			if (!$product_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->language->getId() . "' ORDER BY pd.name ASC");

				$product_data = $query->rows;

				$this->cache->set('product.' . $this->language->getId(), $product_data);
			}

			return $product_data;
		}
	}
	public function getProductsByCategoryId($category_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE pd.language_id = '" . (int)$this->language->getId() . "' AND p2c.category_id = '" . (int)$category_id . "' ORDER BY pd.name ASC");

		return $query->rows;
	}

	public function getProductDescriptions($product_id) {
		$product_description_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");

		foreach ($query->rows as $result) {
			$product_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}

		return $product_description_data;
	}

	public function getProductOptions($product_id) {
		$product_option_data = array();

		$product_option = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order");

		foreach ($product_option->rows as $product_option) {
			$product_option_value_data = array();

			$product_option_value = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value WHERE product_option_id = '" . (int)$product_option['product_option_id'] . "' ORDER BY sort_order");

			foreach ($product_option_value->rows as $product_option_value) {
				$product_option_value_description_data = array();

				$product_option_value_description = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_value_description WHERE product_option_value_id = '" . (int)$product_option_value['product_option_value_id'] . "'");

				foreach ($product_option_value_description->rows as $result) {
					$product_option_value_description_data[$result['language_id']] = array('name' => $result['name']);
				}

				$product_option_value_data[] = array(
					'product_option_value_id' => $product_option_value['product_option_value_id'],
					'language'                => $product_option_value_description_data,
         			'price'                   => $product_option_value['price'],
         			'prefix'                  => $product_option_value['prefix'],
					'sort_order'              => $product_option_value['sort_order']
				);
			}

			$product_option_description_data = array();

			$product_option_description = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_option_description WHERE product_option_id = '" . (int)$product_option['product_option_id'] . "'");

			foreach ($product_option_description->rows as $result) {
				$product_option_description_data[$result['language_id']] = array('name' => $result['name']);
			}

        	$product_option_data[] = array(
        		'product_option_id'    => $product_option['product_option_id'],
				'language'             => $product_option_description_data,
				'product_option_value' => $product_option_value_data,
				'sort_order'           => $product_option['sort_order']
        	);
      	}

		return $product_option_data;
	}

	public function getProductImages($product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");

		return $query->rows;
	}

	public function getProductDiscounts($product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' ORDER BY quantity, price");

		return $query->rows;
	}

	public function getProductDownloads($product_id) {
		$product_download_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");

		foreach ($query->rows as $result) {
			$product_download_data[] = $result['download_id'];
		}

		return $product_download_data;
	}

	public function getProductCategories($product_id) {
		$product_category_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");

		foreach ($query->rows as $result) {
			$product_category_data[] = $result['category_id'];
		}

		return $product_category_data;
	}

	public function getProductRelated($product_id) {
		$product_related_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");

		foreach ($query->rows as $result) {
			$product_related_data[] = $result['related_id'];
		}

		return $product_related_data;
	}

	public function getTotalProducts($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->language->getId() . "'";

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND pd.name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_model']) && !is_null($data['filter_model'])) {
			$sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
		}

		if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
			$sql .= " AND p.quantity = '" . $this->db->escape($data['filter_quantity']) . "'";
		}

		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}

	public function getTotalProductsByStockStatusId($stock_status_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE stock_status_id = '" . (int)$stock_status_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE image_id = '" . (int)$image_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByTaxClassId($tax_class_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE tax_class_id = '" . (int)$tax_class_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByWeightClassId($weight_class_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE weight_class_id = '" . (int)$weight_class_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByMeasurementClassId($measurement_class_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE measurement_class_id = '" . (int)$measurement_class_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByOptionId($option_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_option WHERE option_id = '" . (int)$option_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByDownloadId($download_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_download WHERE download_id = '" . (int)$download_id . "'");

		return $query->row['total'];
	}

	public function getTotalProductsByManufacturerId($manufacturer_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");

		return $query->row['total'];
	}

	//-------------------------------------------------------------------------//
	//get child catalog for combobox
	//-------------------------------------------------------------------------//
	public function getChildCategories($parent_id) {
	$SQL = "SELECT distinct c.category_id, cd.name " .
			"	FROM " . DB_PREFIX . "category c " .
				"LEFT JOIN " . DB_PREFIX . "category_description cd " .
					"ON (c.category_id = cd.category_id) " .
			"WHERE c.parent_id = '" . (int)$parent_id . "' " .
			"ORDER BY c.sort_order, cd.name ASC";
		$query = $this->db->query($SQL);
		return $query->rows;
	}
	//-------------------------------------------------------------------------//
	// get parent catalog
	//-------------------------------------------------------------------------//
	public function getParentCategories() {
	$SQL = " SELECT distinct c.category_id as catalog_id, cd.name " .
			"	FROM " . DB_PREFIX . "category c " .
				" LEFT JOIN " . DB_PREFIX . "category_description cd " .
					"ON (c.category_id = cd.category_id) " .
			" WHERE c.parent_id = 0 " .
			" ORDER BY c.sort_order ASC ";
		$query = $this->db->query($SQL);
		return $query->rows;
	}

	//-------------------------------------------------------------------------//
	// Convert string
	//-------------------------------------------------------------------------//
	public function convertToSeo($str) {
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
		$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
		$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
		$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
		$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
		$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
		$str = preg_replace("/(Đ)/", 'D', $str);
		$str = preg_replace('/ +/', '-', str_replace("&*#39;","",$str));
		return $str;
	}
}
?>